package GUI;

import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import Entity.User;

/**
 * @author aviat
 *
 */
public class ViewWorkerDetails extends JPanel {
        
        private static final long serialVersionUID = 1L;
        private JComboBox<String> userNameComboBox;
        private JButton btnBack;
        private JComboBox<String> LastNameComboBox;
        private JTextField PhoneNumberTextField;
        private JTextField EmailAddressTextField;
        private JLabel iconlabel;
        private JLabel lblWorkerId;
        private JComboBox<String> WorkerIDcomboBox;
        
        private ArrayList<User> m_allUsers;
        private ArrayList<User> m_filteredUsers;
        private Object syncObject;
        
        private boolean m_isChangeAllowed;
        private JButton btnRefresh;
        private JComboBox<String> AddresscomboBox;
        private JLabel lblPosition;
        private JComboBox<String> PositioncomboBox;
        
        public ViewWorkerDetails() {
                setBackground(new Color(255, 51, 0));
                setLayout(null);
                userNameComboBox = new JComboBox<String>();
                userNameComboBox.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                synchronized (syncObject) {
                                        if(isChangeAllowed())
                                                filterList(userNameComboBox.getSelectedItem().toString(),"FNAME");
                            }
                    }
            });
            LastNameComboBox = new JComboBox<String>();
            LastNameComboBox.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                            synchronized (syncObject) {
                                    if(isChangeAllowed())
                                            filterList(LastNameComboBox.getSelectedItem().toString(),"LNAME");
                            }
                    }
            });
            AddresscomboBox = new JComboBox<String>();
            PhoneNumberTextField = new JTextField();
            EmailAddressTextField = new JTextField();
            btnBack = new JButton("Back");
            btnBack.setIcon(new ImageIcon(ViewWorkerDetails.class.getResource("/resources/back22.png")));
            
            WorkerIDcomboBox = new JComboBox<String>();
            WorkerIDcomboBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                  synchronized (syncObject) {
                     if(isChangeAllowed())
                       {
                    	 filterList(WorkerIDcomboBox.getSelectedItem().toString(),"UNAME");
                       }           
                  }
                }
            });
            
            PositioncomboBox = new JComboBox<String>();
            PositioncomboBox.addActionListener(new ActionListener() {
            	public void actionPerformed(ActionEvent e) {
            		synchronized(syncObject){
            			if(isChangeAllowed()){
            				filterList(PositioncomboBox.getSelectedItem().toString(), "Position");
            			}
            		}
            	}
            });
            
            initialize();
    }
    

    private void initialize()
    {
        PhoneNumberTextField.setEditable(false);
        EmailAddressTextField.setEditable(false);
        
        JLabel lblWorkerDetails = new JLabel("Worker Details");
        lblWorkerDetails.setForeground(new Color(0, 204, 255));
        lblWorkerDetails.setFont(new Font("Tahoma", Font.BOLD, 16));
        lblWorkerDetails.setBounds(10, 11, 139, 44);
        add(lblWorkerDetails);
        
        JLabel lblName = new JLabel("First Name");
        lblName.setForeground(Color.WHITE);
        lblName.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblName.setBounds(10, 143, 107, 27);
        add(lblName);
        
        userNameComboBox.setBounds(121, 144, 139, 27);
        AutoCompletion.enable(this.userNameComboBox);
        add(userNameComboBox);
        
        btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
        btnBack.setBounds(10, 462, 101, 27);
        add(btnBack);
        
        JLabel lblLastName = new JLabel("Last Name");
        lblLastName.setForeground(Color.WHITE);
        lblLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblLastName.setBounds(10, 211, 107, 27);
        add(lblLastName);
        
        
        LastNameComboBox.setBounds(121, 212, 139, 27);
        AutoCompletion.enable(this.LastNameComboBox);
        add(LastNameComboBox);
        
        JLabel lblPhoneNumber = new JLabel("Phone Number");
        lblPhoneNumber.setForeground(Color.WHITE);
        lblPhoneNumber.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblPhoneNumber.setBounds(10, 355, 107, 30);
        add(lblPhoneNumber);
        
        JLabel lblEmailAddress = new JLabel("Email Address");
        lblEmailAddress.setForeground(Color.WHITE);
        lblEmailAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblEmailAddress.setBounds(337, 143, 107, 27);
        add(lblEmailAddress);
        
        
        PhoneNumberTextField.setBounds(121, 356, 139, 30);
        add(PhoneNumberTextField);
        PhoneNumberTextField.setColumns(10);
        
        
        EmailAddressTextField.setBounds(454, 144, 139, 27);
        add(EmailAddressTextField);
        EmailAddressTextField.setColumns(10);
        
        JLabel lblHomeAddress = new JLabel("Home Address");
        lblHomeAddress.setForeground(Color.WHITE);
        lblHomeAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblHomeAddress.setBounds(332, 211, 107, 27);
        add(lblHomeAddress);
        
        
        ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
        ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
        
        lblWorkerId = new JLabel("Worker ID");
        lblWorkerId.setForeground(Color.WHITE);
        lblWorkerId.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblWorkerId.setBounds(10, 283, 101, 27);
        add(lblWorkerId);
        
        
        WorkerIDcomboBox.setBounds(121, 284, 139, 27);
        AutoCompletion.enable(this.WorkerIDcomboBox);
        add(WorkerIDcomboBox);
        
        btnRefresh = new JButton("Refresh");
        btnRefresh.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		synchronized (syncObject) {
                    if(isChangeAllowed()){
                    	PhoneNumberTextField.setText("");
                    	EmailAddressTextField.setText("");
                    	filterList(null,"ALL");
                    }
        		}
        	}
        });
        btnRefresh.setIcon(new ImageIcon(ViewWorkerDetails.class.getResource("/resources/arrow-refresh-2-icon.png")));
        btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
        btnRefresh.setBounds(289, 427, 119, 27);
        add(btnRefresh);
        
        
        AddresscomboBox.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		synchronized (syncObject) {
                    if(isChangeAllowed())
                      {
                   	 filterList(AddresscomboBox.getSelectedItem().toString(),"ADDRESS");
                      }           
                 }
        	}
        	
        });
        AddresscomboBox.setBounds(454, 212, 139, 27);
        add(AddresscomboBox);
        
        lblPosition = new JLabel("Position");
        lblPosition.setForeground(Color.WHITE);
        lblPosition.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblPosition.setBounds(337, 287, 107, 27);
        add(lblPosition);
        
        
      
        PositioncomboBox.setBounds(454, 287, 139, 27);
        add(PositioncomboBox);
        
        iconlabel = new JLabel("");
        iconlabel.setIcon(LOGO);
        iconlabel.setBounds(0, 0, 786, 500);
        add(iconlabel);
    }

    /**
     * @return the btnBack
     */
    public JButton getBtnBack() {
            return btnBack;
    }

    /**
     * @param btnBack the btnBack to set
     */
    public void setBtnBack(JButton btnBack) {
            this.btnBack = btnBack;
    }

    /**
     * @return the phoneNumberTextField
     */
    public JTextField getPhoneNumberTextField() {
            return PhoneNumberTextField;
    }

    /**
     * @param phoneNumberTextField the phoneNumberTextField to set
     */
    public void setPhoneNumberTextField(JTextField phoneNumberTextField) {
            PhoneNumberTextField = phoneNumberTextField;
    }

    /**
     * @return the emailAddressTextField
     */
    public JTextField getEmailAddressTextField() {
            return EmailAddressTextField;
    }

    /**
     * @param emailAddressTextField the emailAddressTextField to set
     */
    public void setEmailAddressTextField(JTextField emailAddressTextField) {
            EmailAddressTextField = emailAddressTextField;
    }

    /**
     * @return the workerIDcomboBox
     */
    public JComboBox<String> getWorkerIDcomboBox() {
            return WorkerIDcomboBox;
    }

    /**
     * @param workerIDcomboBox the workerIDcomboBox to set
     */
    public void setWorkerIDcomboBox(JComboBox<String> workerIDcomboBox) {
            WorkerIDcomboBox = workerIDcomboBox;
    }


    /**
     * @return the userNameComboBox
     */
    public JComboBox<String> getUserNameComboBox() {
            return userNameComboBox;
    }


    /**
     * @param userNameComboBox the userNameComboBox to set
     */
    public void setUserNameComboBox(JComboBox<String> userNameComboBox) {
            this.userNameComboBox = userNameComboBox;
    }


    /**
     * @return the lastNameComboBox
     */
    public JComboBox<String> getLastNameComboBox() {
            return LastNameComboBox;
    }


    /**
     * @param lastNameComboBox the lastNameComboBox to set
     */
    public void setLastNameComboBox(JComboBox<String> lastNameComboBox) {
            LastNameComboBox = lastNameComboBox;
    }
    public void setUsers(ArrayList<User> allUsers) {
            m_allUsers = allUsers;          
            m_filteredUsers = new ArrayList<User>();
            syncObject = new Object();
            filterList(null,"ALL");
    }
    
    private void filterList(String selectedItem, String scope)
    {
       synchronized (syncObject) {
           m_filteredUsers.clear();
                    
           switch(scope)
           {
            	case "FNAME":
                     for(User user : m_allUsers)
                     {
                        if(user.getName().equals(selectedItem))
                         {
                            m_filteredUsers.add(user);
                          }
                     }
                     break;
            	case "LNAME":
            		for(User user : m_allUsers)
                    {
                      if(user.getLName().equals(selectedItem))
                      {
                         m_filteredUsers.add(user);
                      }
                    }
                    break;
                case "UNAME":
                    for(User user : m_allUsers)
                    {
                      if(String.valueOf(user.getUserName()).equals(selectedItem))
                       {
                    	  	m_filteredUsers.add(user);
                       }
                    }
                    break;
                case "Position":
                	for(User user : m_allUsers)
                	{
                		if(user.getRole().equals(selectedItem))
                		{
                			m_filteredUsers.add(user);
                		}
                	}
                	break;
                case "ADDRESS":
                    for(User user : m_allUsers)
                    {
                      if(user.getAddress().equals(selectedItem))
                       {
                    	  	m_filteredUsers.add(user);
                       }
                    }
                    break;
                case "ALL":
                    m_filteredUsers.addAll(m_allUsers);
                    break;
              }
         }
      m_isChangeAllowed = false;
     filterComboBox(userNameComboBox,"FNAME");
     filterComboBox(LastNameComboBox,"LNAME");
     filterComboBox(WorkerIDcomboBox,"UNAME");
     filterComboBox(AddresscomboBox,"ADDRESS");
     filterComboBox(PositioncomboBox,"Position");
    
     if(userNameComboBox.getItemCount()==1&&LastNameComboBox.getItemCount()==1&&WorkerIDcomboBox.getItemCount()==1&&AddresscomboBox.getItemCount()==1)
    	 setWorkerDetails();
     
      m_isChangeAllowed = true;

}
    
    private void filterComboBox(JComboBox<String> combo, String scope)
    {
      combo.removeAllItems();
      synchronized (syncObject) {
      
    	  for(User user : m_filteredUsers)
          {
               switch(scope)
               {
                case "FNAME":
                   combo.addItem(user.getName());
                   break;
                case "LNAME":
                   combo.addItem(user.getLName());
                   break;
                case "UNAME":
                   combo.addItem(String.valueOf(user.getUserName()));
                   break;
                case "Position":
                	combo.addItem(user.getRole());
                	break;
                case "ADDRESS":
                    combo.addItem(user.getAddress());
                    break;
               }
          }
     }
}
    /**
     * @return the m_isChangeAllowed
     */
    private boolean isChangeAllowed()
    {
            return m_isChangeAllowed;
    }
	/**
	 * This method add the customers details
	 */
	public void setWorkerDetails(){
		
		for(User u : m_filteredUsers){
			
			if(u.getUserName()==Integer.parseInt(WorkerIDcomboBox.getSelectedItem().toString()))
			{
				EmailAddressTextField.setText(u.getEmail());
				PhoneNumberTextField.setText(u.getPhoneNum());
			}
			break;
		}
	}

    /**
     * @return the btnRefresh
     */
    public JButton getBtnRefresh() {
            return btnRefresh;
    }


    /**
     * @param btnRefresh the btnRefresh to set
     */
    public void setBtnRefresh(JButton btnRefresh) {
            this.btnRefresh = btnRefresh;
    }




    /**
     * @return the m_allUsers
     */
    public ArrayList<User> getM_allUsers() {
            return m_allUsers;
    }


    /**
     * @param m_allUsers the m_allUsers to set
     */
    public void setM_allUsers(ArrayList<User> m_allUsers) {
            this.m_allUsers = m_allUsers;
    }


    /**
     * @return the m_filteredUsers
     */
    public ArrayList<User> getM_filteredUsers() {
            return m_filteredUsers;
    }


    /**
     * @param m_filteredUsers the m_filteredUsers to set
     */
    public void setM_filteredUsers(ArrayList<User> m_filteredUsers) {
            this.m_filteredUsers = m_filteredUsers;
    }


    /**
     * @return the m_isChangeAllowed
     */
    public boolean isM_isChangeAllowed() {
            return m_isChangeAllowed;
    }


    /**
     * @param m_isChangeAllowed the m_isChangeAllowed to set
     */
        public void setM_isChangeAllowed(boolean m_isChangeAllowed) {
                this.m_isChangeAllowed = m_isChangeAllowed;
        }


	/**
	 * @return the addresscomboBox
	 */
	public JComboBox<String> getAddresscomboBox() {
		return AddresscomboBox;
	}


	/**
	 * @param addresscomboBox the addresscomboBox to set
	 */
	public void setAddresscomboBox(JComboBox<String> addresscomboBox) {
		AddresscomboBox = addresscomboBox;
	}


	/**
	 * @return the positioncomboBox
	 */
	public JComboBox<String> getPositioncomboBox() {
		return PositioncomboBox;
	}


	/**
	 * @param positioncomboBox the positioncomboBox to set
	 */
	public void setPositioncomboBox(JComboBox<String> positioncomboBox) {
		PositioncomboBox = positioncomboBox;
	}
}